/**
 * 要素服务编辑类
 *
 * 1.提供要素添加
 * 2.提供要素删除
 * 3.提供要素更新
 *
 */

export default class SmQuery {

  constructor() {
  }

  /**
   *  添加要素
   * @param url              服务地址
   * @param features         要素集合
   * @param dataSourceName   数据源名称
   * @param dataSetName      数据集名称
   * @param isBatch          是否批量
   * @param returnContent    是否返回内容
   * @param callback         回调接口
   */
  static addFeatures(url, features, dataSourceName, dataSetName, isBatch, returnContent) {
    return new Promise((resolve, reject) => {
      let featureService = new mapboxgl.supermap.FeatureService(url);
      let addFeatureParams = new SuperMap.EditFeaturesParameters({
        features: features,
        dataSourceName: dataSourceName,
        dataSetName: dataSetName,
        editType: SuperMap.EditType.ADD,
        isUseBatch: isBatch,
        returnContent: returnContent
      });
      featureService.editFeatures(addFeatureParams, (res, err) => {
        if (err) {
          reject(err);
          return;
        }
        resolve(res);
      });
    });

  }

  /**
   * 删除要素
   * @param url             服务地址
   * @param features        要素集合
   * @param dataSourceName  数据源名称
   * @param dataSetName     数据集名称
   * @param isBatch         是否批量
   * @param ids             要素ID集合
   * @param returnContent   是否返回内容
   * @param callback        回调接口
   */
  static removeFeatures(url, features, dataSourceName, dataSetName, isBatch, ids, returnContent) {
    return new Promise((resolve, reject) => {
      let featureService = new mapboxgl.supermap.FeatureService(url);
      let addFeatureParams = new SuperMap.EditFeaturesParameters({
        features: features,
        dataSourceName: dataSourceName,
        dataSetName: dataSetName,
        editType: SuperMap.EditType.DELETE,
        isUseBatch: isBatch,
        IDs: ids,
        returnContent: returnContent
      });
      featureService.editFeatures(addFeatureParams, (res, err) => {
        if (err) {
          reject(err);
          return;
        }
        resolve(res);
      });
    });
  }

  /**
   * 更新要素
   * @param url                      服务地址
   * @param features                 要素集合
   * @param dataSourceName           数据源名称
   * @param dataSetName              数据集名称
   * @param isBatch                  是否批量
   * @param returnContent            是否返回内容
   * @param callback                 回调接口
   */
  static updateFeatures(url, features, dataSourceName, dataSetName, isBatch, returnContent) {
    return new Promise((resolve, reject) => {
      let featureService = new mapboxgl.supermap.FeatureService(url);
      let addFeatureParams = new SuperMap.EditFeaturesParameters({
        features: features,
        dataSourceName: dataSourceName,
        dataSetName: dataSetName,
        editType: SuperMap.EditType.UPDATE,
        isUseBatch: isBatch,
        returnContent: returnContent
      });
      featureService.editFeatures(addFeatureParams, (res, err) => {
        console.log(res, err);
        if (err) {
          reject(err);
          return;
        }
        resolve(res);
      });
    });
  }

  /**
   * sql要素查询
   * @param url
   * @param dataSourceName
   * @param dataSetName
   * @param condition
   * @param callback
   */
  static getFeaturesBySQL(url, dataSourceName, dataSetName, condition) {
    return new Promise((resolve, reject) => {
      let getFeatureParams = new SuperMap.GetFeaturesBySQLParameters({
        toIndex: -1,
        maxFeatures: 9999999, //7393数据是一条线
        datasetNames: [dataSourceName + ':' + dataSetName],
        queryParameter: {
          attributeFilter: condition ? condition : '1=1',
          name: dataSetName
        }
      });
      let featureService = new mapboxgl.supermap.FeatureService(url);
      featureService.getFeaturesBySQL(getFeatureParams, (r) => {
        resolve(r);
      });
    });
  }


  static getFeatureField(url, dataSourceName, dataSetName) {
    return new Promise((resolve, reject) => {
      const param = new SuperMap.FieldParameters({datasource: dataSourceName, dataset: dataSetName})
      const fieldService = new mapboxgl.supermap.FieldService(url);
      fieldService.getFields(param, function (result) {
        console.log(result);
      });
    })
  }


  /**
   * 属性查询服务,返回满足基本查询条件的数据 （sql语句查询）
   * @param layerName
   * @param sql
   * @param url
   * @param callback
   */
  static mapServiceLayerSearchBySQL(layerName, sql, url, start, size) {
    return new Promise((resolve, reject) => {
      let param = new SuperMap.QueryBySQLParameters({
        queryParams: {
          name: layerName,
          attributeFilter: sql
        },
        startRecord: start,
        expectCount: size,
      });
      new mapboxgl.supermap.QueryService(url).queryBySQL(param, res => {
        if (res.result) {
          resolve(res);
        } else {
          reject('查询结果错误');
        }
      })
    });
  }


  /**
   * 简单统计查询服务,返回统计类别及数量、长度、面积等 （sql语句查询）
   * @param url
   * @param layerName
   * @param field
   * @returns {Promise<any>}
   */
  static mapServiceLayerStaticsBySQL(url,layerName,field,attributeFilter) {
    let attributeFilterParam = "1=1";
    if(attributeFilter){
      attributeFilterParam = attributeFilter
    }
    //console.log(attributeFilterParam);
    return new Promise((resolve, reject) => {
      let param = new SuperMap.QueryBySQLParameters({
        queryParams: {
          name: layerName,
          attributeFilter:attributeFilterParam,
          fields:["count(*) as count",field],
          groupby:field 
        }
      });
      new mapboxgl.supermap.QueryService(url).queryBySQL(param, res => {
        if (res.result) {
          resolve(res);
        } else {
          reject();
        }
      })
    });
  }


  /**
   * 属性查询服务（geometry查询）
   * @param layerName
   * @param geo
   * @param url
   * @param callback
   */
  static mapServiceLayerSearchByGeo(layerName, geo, url) {
    return new Promise((resolve, reject) => {
      var param = new SuperMap.QueryByGeometryParameters({
        queryParams: {name: layerName},
        geometry: geo
      });
      let queryService = new mapboxgl.supermap.QueryService(url);
      queryService.queryByGeometry(param, (serviceResult) => {
        if (serviceResult.result) {
          var features = serviceResult.result.recordsets[0].features.features;
          resolve(features);
        } else {
          reject();
        }
      });
    });
  }



  static getRiverFeaturesBySQL(url, condition) {
    return new Promise((resolve, reject) => {
      let getFeatureParams = new SuperMap.GetFeaturesBySQLParameters({
        toIndex: -1,
        maxFeatures: 9999999, //7393数据是一条线
        datasetNames: ["hzz:river","hzz:lake"],
        queryParameter: {
          attributeFilter: condition ? condition : '1=1'
        }
      });
      let featureService = new mapboxgl.supermap.FeatureService(url);
      featureService.getFeaturesBySQL(getFeatureParams, (r) => {
        resolve(r);
      });
    });
  }

}




